package acwing._2LanQiao._5BinaryIndexedTree_SegmentTree;
/**
 * @Project : ProgrammingExercises
 * @Package : ACWing._2LanQiao._5BinaryIndexedTree_SegmentTree
 * @File : Q1270.java
 * @Author : WangRuoyu
 * @Date : 2023/3/17 11:04
 */

import java.io.*;

public class Q1270 {
    static long[] d;
    static int n;
    static int m;
    static segmentTreeMax[] tr;

    static void pushup(int u) {
        tr[u].max = Math.max(tr[u << 1].max, tr[u << 1 | 1].max);
    }

    static void build(int u, int l, int r) {
        if (l == r) {
            tr[u] = new segmentTreeMax(l, r, d[l]);
        } else {
            tr[u] = new segmentTreeMax(l, r, 0);
            int mid = (l + r) >> 1;
            build(u << 1, l, mid);
            build(u << 1 | 1, mid + 1, r);
            pushup(u);
        }
    }

    static long query(int u, int l, int r) {
        if (tr[u].l >= l && tr[u].r <= r) {
            return tr[u].max;
        }
        int mid = (tr[u].l + tr[u].r) >> 1;
        long max = Long.MIN_VALUE;
        if (l <= mid) max = Math.max(max, query(u << 1, l, r));
        if (r > mid) max = Math.max(max, query(u << 1 | 1, l, r));
        return max;
    }

    static void modify(int u, int x, int c) {
        if (tr[u].l == tr[u].r) {
            tr[u].max = Math.max(tr[u].max, c);
        }
        int mid = (tr[u].l + tr[u].r) >> 1;
        if (x <= mid) modify(u << 1, x, c);
        else modify(u << 1 | 1, x, c);
        pushup(u);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] words = br.readLine().split(" ");

        n = Integer.parseInt(words[0]);
        m = Integer.parseInt(words[1]);
        d = new long[n + 1];
        tr = new segmentTreeMax[4 * n + 1];

        words = br.readLine().split(" ");
        for (int i = 1; i <= n; ++i) {
            d[i] = Integer.parseInt(words[i - 1]);
        }

        build(1, 1, n);

        for (int i = 0; i < m; ++i) {
            words = br.readLine().split(" ");
            int a = Integer.parseInt(words[0]);
            int b = Integer.parseInt(words[1]);

            out.write(query(1, Math.min(a, b), Math.max(a, b)) + "\n");
        }
        out.flush();
    }
}
